﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.Entities;
using MovieRental.DataInterfaces;
using MovieRental.SqlRepositoryLibrary.Converters;

namespace MovieRental.SqlRepositoryLibrary
{
    public class SqlMovieRepository : SqlRepository<MovieInfo, Guid, MovieConverter>, IMovieRepository
    {
        public SqlMovieRepository(string connectionString)
            : base(connectionString)
        {
        }

        protected override bool Contains(MovieInfo entity)
        {
            return Contains(entity.Id);
        }

        public override void Delete(MovieInfo entity)
        {
            Delete(entity.Id);
        }

        public bool MovieExists(Guid id)
        {
            return Contains(id);
        }

        public override void Delete(Guid id)
        {
            var editionRepository = new SqlEditionRepository(_connectionString);
            foreach (var edition in editionRepository.LoadAll().Where(x => x.MovieId == id))
                editionRepository.Delete(edition);
            base.Delete(id);
        }
    }
}
